package org.maven.dependencybrowser.gui;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.maven.dependencybrowser.model.Artifact;

import javax.swing.*;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

public class DependencyTreeRenderer extends DefaultTreeCellRenderer
{

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)
    {
        DefaultTreeCellRenderer r = (DefaultTreeCellRenderer) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        Artifact artifact = (Artifact) value;
        r.setForeground(Color.BLACK);
        r.setBackground(Color.WHITE);
        r.setOpaque(true);
        if (selected)
        {
            r.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
        }
        else
        {
            r.setBorder(null);
        }
        if (Artifact.SELECTED_NONE == artifact.getSelected())
        {
            r.setFont(r.getFont().deriveFont(Font.PLAIN));
        }
        else
        {
            r.setFont(r.getFont().deriveFont(Font.BOLD));
            if (Artifact.SELECTED_PRINCIPLE == artifact.getSelected())
            {
                r.setBackground(Color.YELLOW);
            }
        }
        return r;
    }

    /**
     * Add this override to recalculate the width of this JLabel. The super class default
     * behaviour miscalculates the width, and so the '...'  can appear. Instead, we 'simulate'
     * the FontMetrics' stringWidth() method, by using charWidth(), plus some initialization and padding
     */
    public Dimension getPreferredSize()
    {
        Dimension dim = super.getPreferredSize();
        FontMetrics fm = getFontMetrics(getFont());
        char[] chars = getText().toCharArray();
        /* 
         * Initialize the width value to take into account any icons and gaps. Don't try to get the 
         * Icon's width programatically from within this method, as an infinite loop will result.  I've 
         * just assumed a width of 16 here, but you could (should?) save the actual value in 
         * getTreeCellRendererComponent() and retrieve it here, if you wanted.
         */
        int w = getIconTextGap() + 16;
        for (int i = 0; i < chars.length; i++)
        {
            w += fm.charWidth(chars[i]);
        }
        w += getText().length();
        dim.width = w;
        return dim;
    }
}
